package com.gimi.cloud.jimi.manage.service.prod.impl;

import com.gimi.cloud.jimi.common.utils.MyBeanUtils;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.commonService.common.JmOperationLogService;
import com.gimi.cloud.jimi.dao.dao.product.SecurityInstructionsDao;
import com.gimi.cloud.jimi.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.jimi.dto.base.BaseDTO;
import com.gimi.cloud.jimi.dto.product.SecurityInstructionsDTO;
import com.gimi.cloud.jimi.entity.product.SecurityInstructionsEntity;
import com.gimi.cloud.jimi.manage.service.prod.SecurityInstructionsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther loongs
 * @date 2018/1/30  13:29
 */
@Service
public class SecurityInstructionsServiceImpl implements SecurityInstructionsService {

    private SecurityInstructionsDao securityInstructionsDao;
    private JmOperationLogService jmOperationLogService;

    @Autowired
    public SecurityInstructionsServiceImpl(SecurityInstructionsDao securityInstructionsDao, JmOperationLogService jmOperationLogService) {
        this.securityInstructionsDao = securityInstructionsDao;
        this.jmOperationLogService = jmOperationLogService;
    }

    @Override
    public PageInfo<SecurityInstructionsDTO> getSecurityInstructionsPageList(SecurityInstructionsDTO securityInstructionsDTO) {
        PageHelper.startPage(securityInstructionsDTO.getCurrentPage(), securityInstructionsDTO.getPageSize());
        String sql = "select * from ec_security_instructions where 1=1 and title like '%'||:title||'%'";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParamObject(securityInstructionsDTO);
        List<SecurityInstructionsDTO> results = securityInstructionsDao.getSecurityInstructionsPageList(queryParam);
        for (SecurityInstructionsDTO securityInstructionsDTO1 : results) {
            securityInstructionsDTO1.setImgUrl(AliOSSUtil.getFileUrl(securityInstructionsDTO1.getImgUrl(), AliOSSUtil.getNoEffictDateByDay(1)));
        }
        PageInfo<SecurityInstructionsDTO> page = new PageInfo<>(results);
        return page;
    }

    @Override
    public SecurityInstructionsDTO getSecurityInstructionsSingle(SecurityInstructionsDTO securityInstructionsDTO) {
        SecurityInstructionsEntity securityInstructionsEntity = securityInstructionsDao.selectByPrimaryKey(securityInstructionsDTO.getId());
        SecurityInstructionsDTO securityInstructionsDTO1 = new SecurityInstructionsDTO();
        MyBeanUtils.copyPropertiesIgnoreNull(securityInstructionsEntity, securityInstructionsDTO1);
        //短路径改成长路径返回
        securityInstructionsDTO1.setImgUrl(AliOSSUtil.getFileUrl(securityInstructionsDTO1.getImgUrl(), AliOSSUtil.getNoEffictDateByDay(1)));
        //添加操作日志
        jmOperationLogService.insertQueryLog(new BaseDTO(),"getSecurityInstructionsSingle","ec_security_instructions",
                securityInstructionsEntity.getId()+"","获得单个保障说明");
        return securityInstructionsDTO1;
    }

    @Override
    public boolean addOrEditSecurityInstructions(SecurityInstructionsDTO securityInstructionsDTO) {
        boolean flag = false;
        SecurityInstructionsEntity securityInstructionsEntity = new SecurityInstructionsEntity();
        //长路径改成短路径，存入
        securityInstructionsDTO.setImgUrl(AliOSSUtil.getFilePath(securityInstructionsDTO.getImgUrl()));
        //如果id为空，说明是add，反之则是edit
        if (StringUtil.isBlank(securityInstructionsDTO.getId())) {
            MyBeanUtils.copyPropertiesIgnoreNull(securityInstructionsDTO, securityInstructionsEntity);
            //下面第三个属性添加的时候要清除
            securityInstructionsEntity.setUpdateTime(null);
            securityInstructionsEntity.setUpdateUid(null);
            securityInstructionsEntity.setUpdateUname(null);
            int result = securityInstructionsDao.insertSelective(securityInstructionsEntity);
            if (result > 0) {
                //添加操作日志
                BaseDTO baseDTO=new BaseDTO();
                BeanUtils.copyProperties(securityInstructionsDTO,baseDTO);
                jmOperationLogService.insertAddLog(baseDTO,null,"addOrEditSecurityInstructions",
                        "ec_security_instructions","添加保障说明");
                flag = true;
            }
        } else {
            //把原来添加时的属性要保留
            SecurityInstructionsEntity oldSecurityInstructionsEntity = securityInstructionsDao.selectByPrimaryKey(securityInstructionsDTO.getId());
            securityInstructionsDTO.setAddTime(oldSecurityInstructionsEntity.getAddTime());
            securityInstructionsDTO.setAddUid(oldSecurityInstructionsEntity.getAddUid());
            securityInstructionsDTO.setAddUname(oldSecurityInstructionsEntity.getAddUname());
            MyBeanUtils.copyPropertiesIgnoreNull(securityInstructionsDTO, securityInstructionsEntity);
            int result = securityInstructionsDao.updateByPrimaryKey(securityInstructionsEntity);
            if (result > 0) {
                //添加操作日志
                BaseDTO baseDTO=new BaseDTO();
                BeanUtils.copyProperties(securityInstructionsDTO,baseDTO);
                jmOperationLogService.insertUpdateLog(baseDTO,"addOrEditSecurityInstructions","ec_security_instructions",
                        securityInstructionsEntity.getId()+"","修改保障说明");
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public boolean delSecurityInstructions(SecurityInstructionsDTO securityInstructionsDTO) {
        int flag = securityInstructionsDao.deleteByPrimaryKey(securityInstructionsDTO.getId());
        if (flag > 0) {
            //添加操作日志
            jmOperationLogService.insertDeleteLog(securityInstructionsDTO.getAddUid(),"delSecurityInstructions","ec_security_instructions",
                    securityInstructionsDTO.getId()+"","删除指定的保障说明");
            return true;
        }
        return false;
    }
}
